package cn.zptc.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cn.zptc.dao.EmployeeDAO;
import cn.zptc.entity.Employee;

/**
 * Servlet implementation class GetAllEmployeesServlet
 */
/**
 * 处理"/getAllEmployees"请求。
 * 通过EmployeeDAO查询出所有员工，然后转化为json字符串，
 * 最后把json串向外输出。
 */
@WebServlet("/getAllEmployees")
public class GetAllEmployeesServlet extends HttpServlet {
 
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response){
        //1. 调用dao查询
        EmployeeDAO dao = new EmployeeDAO();
        try {
            List<Employee> allEmployees = dao.selectAllEmployees();
            //2. 将集合转换为JSON数据   序列化
            String jsonString = JSON.toJSONString(allEmployees);
            //3. 响应数据
            response.setContentType("text/json;charset=utf-8");
            response.getWriter().write(jsonString);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
